require('events').EventEmitter.defaultMaxListeners = 50;
const path = require('path');
const env = process.env;
const isProd = env.MODE === 'prod';

// qiyu 2020-2-29
let envFileName = '';
if (['DEV', 'TEST', 'UAT', 'PRD'].indexOf(env.MODE) > -1) {
  require('dotenv').config({
    path: `.env.${env.MODE}`
  });
  envFileName = `.env.${env.MODE}`;
} else {
  require('dotenv').config();
  envFileName = '.env';
}

const apiServer = process.env.API_SERVER;
const guokeChannelServeDev = process.env.guokeChannelServeDev;

console.log('当前环境变量值', process.env.API_SERVER, guokeChannelServeDev);

const config = {
  env: {
    '/deepexi-tenant': apiServer,
    '/deepexi-permission': apiServer,
    '/m-mall-center/': guokeChannelServeDev,
    '/m-staff-center/api/v1': guokeChannelServeDev,
    '/guoke-channel-aggregation-center/': guokeChannelServeDev,
    '/guoke-deepexi-staff-center/': guokeChannelServeDev,
    '/guoke-deepexi-storage-center/': guokeChannelServeDev,
    '/guoke-deepexi-file-library/': guokeChannelServeDev,
    '/guoke-trade-aggregation-center/': guokeChannelServeDev,
    '/guoke-deepexi-product-center/': guokeChannelServeDev,
    '/guoke-deepexi-config-center/': guokeChannelServeDev,
    '/guoke-deepexi-interface-center/': guokeChannelServeDev,
    '/guoke-deepexi-marking-center/': guokeChannelServeDev,
    '/guoke-deepexi-message-center/': guokeChannelServeDev,
    '/guoke-deepexi-order-center/': guokeChannelServeDev,
    '/guoke-deepexi-hossyn-order-center/': guokeChannelServeDev,
    '/guoke-deepexi-mall-manager/': guokeChannelServeDev,
    '/guoke-deepexi-auth-center/': guokeChannelServeDev,
    '/guoke-deepexi-dms-order-center/': guokeChannelServeDev
  }
};

let axios = {
  proxy: true
};

// 如果生产指定apiServer, 则使用绝对路径请求api
if (isProd && apiServer) {
  axios = {
    proxy: false,
    baseURL: apiServer
  };
}

module.exports = {
  dir: {
    assets: '../m-front-common/pc/assets',
    layouts: '../m-front-common/pc/layouts',
    middleware: '../m-front-common/pc/middleware',
    pages: 'pages',
    static: '../m-front-common/pc/static',
    store: '../m-front-common/pc/store'
  },
  mode: 'spa',
  env: {
    COOKIE_PATH: process.env.COOKIE_PATH || '/'
  },
  proxy: config.env,
  router: {
    base: process.env.ROUTER_BASE,
    middleware: ['meta'],
    mode: 'hash'
  },
  server: {
    port: 3010,
    host: '0.0.0.0'
  },
  build: {
    publicPath: process.env.PUBLIC_PATH,
    extractCSS: true,
    babel: {
      plugins: [
        [
          'component',
          {
            libraryName: 'element-ui',
            styleLibraryName: 'theme-chalk'
          }
        ]
      ]
    },
    /*
     ** Run ESLint on save
     */
    extend(config, {
      isDev
    }) {
      const alias = (config.resolve.alias = config.resolve.alias || {});
      alias['@api'] = path.join(__dirname, 'm-front-common/pc/api');
      alias['@components'] = path.join(__dirname, 'm-front-common/pc/components');
      alias['@m_components'] = path.join(__dirname, 'm-front-common/pc/m-components');
      alias['@const'] = path.join(__dirname, 'm-front-common/pc/const');
      alias['@mixins'] = path.join(__dirname, 'm-front-common/pc/mixins');
      alias['@assets'] = path.join(__dirname, 'm-front-common/pc/assets');
      alias['@layouts'] = path.join(__dirname, 'm-front-common/pc/layouts');
      alias['@services'] = path.join(__dirname, 'm-front-common/pc/services');
      alias['@pc'] = path.join(__dirname, 'm-front-common/pc');
    }
  },
  /*
   ** Headers of the page
   */
  head: {
    title: '数字化供应链生态平台',
    meta: [{
        charset: 'utf-8'
      },
      {
        name: 'viewport',
        content: 'width=device-width, initial-scale=1'
      },
      {
        'http-equiv': 'x-ua-compatible',
        content: 'IE=edge, chrome=1'
      },
      {
        hid: 'description',
        name: 'description',
        content: '开发平台'
      }
    ],
    link: [{
        rel: 'icon',
        type: 'image/x-icon',
        href: 'https://acn2ustorageaccountm.blob.core.chinacloudapi.cn/guoke-channel-aggregation-center/5hmoqxr9z033lmi2qartn2wifa8274pn.png'
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/m-static-resource/m-icon/m-pc-public/iconfont.css'
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://md.heng-tai.com.cn/guoke-deepexi-utils-server-h5/icon/m-pc-menu-icon/iconfont.css'
      },
      // 国科渠道
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: '//at.alicdn.com/t/font_1242246_wa1wt85uy5.css'
      },
      
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/guoke-deepexi-utils-server-h5/icon/sub-sys-icon/iconfont.css'
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/guoke-deepexi-utils-server-h5/icon/agency-sass-icon/iconfont.css'
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/m-static-resource/m-icon/m-pc-public/iconfont.css',
      },
      {
        rel: 'stylesheet',
        type: 'text/css',
        href: 'https://m.met-ec.com/m-static-resource/m-icon/m-pc-menu/iconfont.css',
      }
    ]
  },
  /*
   ** Customize the progress bar color
   */
  loading: {
    color: '#1890ff'
  },
  styleResources: {
    less: '../m-front-common/pc/assets/m-var.less'
  },
  css: [{
    src: '../m-front-common/pc/assets/m-reset.less',
    lang: 'less'
  }],
  srcDir: 'src/',
  plugins: [{
      src: '../m-front-common/pc/plugins/axios'
    },
    {
      src: '../m-front-common/pc/plugins/element'
    },
    {
      src: '../m-front-common/pc/plugins/confirmCustom/index'
    },
    {
      src: '../m-front-common/pc/plugins/pdfViewer/index'
    },
    {
      src: '../m-front-common/pc/plugins/router'
    },
    {
      src: '../m-front-common/pc/plugins/const'
    },
    {
      src: '../m-front-common/pc/plugins/viewer'
    },
    {
      src: '../m-front-common/pc/plugins/icon-font'
    },
    { src: '../m-front-common/pc/plugins/m-plugins' }
  ],
  modules: [
    '@nuxtjs/style-resources',
    ['@nuxtjs/axios'],
    [
      '@nuxtjs/dotenv',
      {
        path: './',
        filename: envFileName
      }
    ]
  ],
  axios
};